package com.stone.framework.user.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.stone.framework.message.ReqMessage;
import com.stone.framework.message.ResMessage;
import com.stone.framework.message.user.PermissionReqBody;
import com.stone.framework.message.user.PermissionResBody;
import com.stone.framework.user.service.PermissionService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "权限")
@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @ApiOperation(value = "获取树形结构", httpMethod = "POST")
    @PostMapping(value = "/getTree")
    ResMessage<PermissionResBody> getTree(@Valid @RequestBody ReqMessage<Long> reqMessage) {
        PermissionResBody resBody = permissionService.buildTree(reqMessage.getBody());
        return ResMessage.success(resBody);
    }

    @ApiOperation(value = "保存", httpMethod = "POST")
    @PostMapping(value = "/save")
    ResMessage<Void> save(@Valid @RequestBody ReqMessage<PermissionReqBody> reqMessage) {
        permissionService.save(reqMessage.getBody());
        return ResMessage.success();
    }

}
